package formularios;

import consistencia.ValidaCampoNumerico;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.Fornecedor;
import dao.Resultado;
import javax.swing.JOptionPane;
import static dao.BancoDeDados.*;

public class frmCadastroFornecedor extends javax.swing.JInternalFrame {

    private int codigo = 0;

    /** Creates new form frmCadastroFornecedor */
    public frmCadastroFornecedor() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabelCodigo = new javax.swing.JLabel();
        txtCodigo = new ValidaCampoNumerico();
        jLabel2 = new javax.swing.JLabel();
        txtRazaoSocial = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtCNPJ = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTelefone1 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtTelefone2 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtCidade = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtEstado = new javax.swing.JTextField();
        btnNovo = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btnEditar = new javax.swing.JButton();

        setClosable(true);
        setPreferredSize(new java.awt.Dimension(600, 550));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel5.setText("Fornecedores");

        jLabelCodigo.setText("Codigo:");

        txtCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtCodigoFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCodigoFocusLost(evt);
            }
        });

        jLabel2.setText("Nome da empresa:");

        txtRazaoSocial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRazaoSocialActionPerformed(evt);
            }
        });

        jLabel6.setText("CNPJ:");

        txtCNPJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCNPJActionPerformed(evt);
            }
        });

        jLabel3.setText("Razão Social:");

        txtTelefone1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelefone1ActionPerformed(evt);
            }
        });

        jLabel7.setText("Telefone 1:");

        jLabel8.setText("Telefone 2:");

        txtTelefone2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelefone2ActionPerformed(evt);
            }
        });

        jLabel9.setText("Email:");

        txtEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmailActionPerformed(evt);
            }
        });

        jLabel10.setText("Endereço:");

        txtEndereco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEnderecoActionPerformed(evt);
            }
        });

        jLabel12.setText("Cidade:");

        txtCidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCidadeActionPerformed(evt);
            }
        });

        jLabel11.setText("Estado:");

        txtEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEstadoActionPerformed(evt);
            }
        });

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.setEnabled(false);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.setEnabled(false);
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel4.setText("Para buscar digite o codigo do cliente, para inserir novo cliente pessoa jurídica clique em \"Novo\"");

        btnEditar.setText("Editar Cliente");
        btnEditar.setEnabled(false);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabelCodigo)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtTelefone1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtTelefone2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtEmail)
                        .addComponent(txtRazaoSocial)
                        .addComponent(txtEndereco)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtEstado, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtCidade, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)))
                .addContainerGap(47, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(61, 61, 61))
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(166, 166, 166)
                .addComponent(jLabel5)
                .addContainerGap(174, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCodigo)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtTelefone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTelefone2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNovo)
                    .addComponent(btnEditar)
                    .addComponent(btnSalvar)
                    .addComponent(btnExcluir)
                    .addComponent(btnCancelar))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoFocusGained
        limparCampos();
}//GEN-LAST:event_txtCodigoFocusGained

    private void txtCodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoFocusLost

        codigo = (Integer.parseInt("0" + txtCodigo.getText()));

        if (codigo == 0) {
            txtCodigo.requestFocusInWindow();
            return;
        } else {

            Object[] codigo_fornecedor = new Object[1];
            codigo_fornecedor[0] = codigo;

            Resultado rs_fornecedor = executaConsulta("Select * from fornecedor where codigo = ?", codigo_fornecedor);


            Fornecedor fornecedor = new Fornecedor();

            if (rs_fornecedor.qtdRegistros() != 0) {
                fornecedor.setCodigo((Integer) rs_fornecedor.valorCampo(0, "codigo"));
                fornecedor.setNome((String) rs_fornecedor.valorCampo(0, "nome"));
                fornecedor.setCnpj((String) rs_fornecedor.valorCampo(0, "cnpj"));
                fornecedor.setRazao_social((String) rs_fornecedor.valorCampo(0, "razao_social"));
                fornecedor.setTelefone1((String) rs_fornecedor.valorCampo(0, "telefone1"));
                fornecedor.setTelefone2((String) rs_fornecedor.valorCampo(0, "telefone2"));
                fornecedor.setEmail((String) rs_fornecedor.valorCampo(0, "email"));
                fornecedor.setEndereco((String) rs_fornecedor.valorCampo(0, "endereco"));
                fornecedor.setCidade((String) rs_fornecedor.valorCampo(0, "cidade"));
                fornecedor.setEstado((String) rs_fornecedor.valorCampo(0, "estado"));

                txtNome.setText(fornecedor.getNome());
                txtCNPJ.setText(fornecedor.getCnpj());
                txtRazaoSocial.setText(fornecedor.getRazao_social());
                txtTelefone1.setText(fornecedor.getTelefone1());
                txtTelefone2.setText(fornecedor.getTelefone2());
                txtEmail.setText(fornecedor.getEmail());
                txtEndereco.setText(fornecedor.getEndereco());
                txtCidade.setText(fornecedor.getCidade());
                txtEstado.setText(fornecedor.getEstado());

                btnEditar.setEnabled(true);
                btnExcluir.setEnabled(true);
                desabilitarCampos();

            } else {
                codigo = 0;
                JOptionPane.showMessageDialog(this, "Não existe fonecedor cadastrado com esse codigo!");
                txtCodigo.requestFocusInWindow();
            }
        }
    }//GEN-LAST:event_txtCodigoFocusLost

    private void txtCNPJActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCNPJActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCNPJActionPerformed

    private void txtRazaoSocialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRazaoSocialActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtRazaoSocialActionPerformed

    private void txtTelefone1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelefone1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtTelefone1ActionPerformed

    private void txtTelefone2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelefone2ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtTelefone2ActionPerformed

    private void txtEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmailActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtEmailActionPerformed

    private void txtEnderecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEnderecoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtEnderecoActionPerformed

    private void txtCidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCidadeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCidadeActionPerformed

    private void txtEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEstadoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtEstadoActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        limparCampos();
        habilitarCampos();
        btnSalvar.setEnabled(true);
        txtCodigo.setVisible(false);
        jLabelCodigo.setVisible(false);
        txtNome.requestFocusInWindow();

}//GEN-LAST:event_btnNovoActionPerformed

    private void limparCampos() {
        txtCodigo.setText("");
        txtNome.setText("");
        txtCNPJ.setText("");
        txtRazaoSocial.setText("");
        txtTelefone1.setText("");
        txtTelefone2.setText("");
        txtEmail.setText("");
        txtEndereco.setText("");
        txtCidade.setText("");
        txtEstado.setText("");
        codigo = 0;
        btnSalvar.setEnabled(false);
        btnEditar.setEnabled(false);
        btnExcluir.setEnabled(false);
    }

    private void habilitarCampos() {
        txtNome.setEditable(true);
        txtCNPJ.setEditable(true);
        txtRazaoSocial.setEditable(true);
        txtTelefone1.setEditable(true);
        txtTelefone2.setEditable(true);
        txtEmail.setEditable(true);
        txtEndereco.setEditable(true);
        txtCidade.setEditable(true);
        txtEstado.setEditable(true);
    }

    private void desabilitarCampos() {
        txtNome.setEditable(false);
        txtCNPJ.setEditable(false);
        txtRazaoSocial.setEditable(false);
        txtTelefone1.setEditable(false);
        txtTelefone2.setEditable(false);
        txtEmail.setEditable(false);
        txtEndereco.setEditable(false);
        txtCidade.setEditable(false);
        txtEstado.setEditable(false);
    }

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        if (codigo == 0) {
            try {
                String nome = txtNome.getText();
                String cnpj = txtCNPJ.getText();
                String razao_social = txtRazaoSocial.getText();
                String telefone1 = txtTelefone1.getText();
                String telefone2 = txtTelefone2.getText();
                String email = txtEmail.getText();
                String endereco = txtEndereco.getText();
                String cidade = txtCidade.getText();
                String estado = txtEstado.getText();
                Object[] fornecedor = new Object[9];
                fornecedor[0] = nome;
                fornecedor[1] = cnpj;
                fornecedor[2] = razao_social;
                fornecedor[3] = telefone1;
                fornecedor[4] = telefone2;
                fornecedor[5] = email;
                fornecedor[6] = endereco;
                fornecedor[7] = cidade;
                fornecedor[8] = estado;
                
                executaOperacao("insert into fornecedor (nome, cnpj, razao_social, telefone1, telefone2, email, endereco, cidade, estado) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", fornecedor);
                JOptionPane.showMessageDialog(this, "Fornecedor cadastrado com sucesso.");
                Object[] cnpj_fornecedor = new Object[1];
                cnpj_fornecedor[0] = cnpj;
                Resultado rs_fornecedor = executaConsulta("Select * from fornecedor where cnpj = ?", cnpj_fornecedor);
                if (rs_fornecedor.qtdRegistros() != 0) {
                    codigo = ((Integer) rs_fornecedor.valorCampo(0, "codigo"));
                } else {
                    codigo = 0;
                }
                txtCodigo.setText(String.valueOf(codigo));
                desabilitarCampos();
            } catch (Exception ex) {
                Logger.getLogger(frmCadastroFornecedor.class.getName()).log(Level.SEVERE, null, ex);
            }


        } else { // codigo diferente de zero = editar

            String nome = txtNome.getText();
            String cnpj = txtCNPJ.getText();
            String razao_social = txtRazaoSocial.getText();
            String telefone1 = txtTelefone1.getText();
            String telefone2 = txtTelefone2.getText();
            String email = txtEmail.getText();
            String endereco = txtEndereco.getText();
            String cidade = txtCidade.getText();
            String estado = txtEstado.getText();
            desabilitarCampos();
            Object[] fornecedor = new Object[10];
            fornecedor[0] = nome;
            fornecedor[1] = telefone1;
            fornecedor[2] = telefone2;
            fornecedor[3] = email;
            fornecedor[4] = endereco;
            fornecedor[5] = cidade;
            fornecedor[6] = estado;
            fornecedor[7] = cnpj;
            fornecedor[8] = razao_social;
            fornecedor[9] = codigo;
            try {
                executaOperacao("update fornecedor set nome = ?, telefone1 = ?, telefone2 = ?, email = ?, endereco = ?, cidade = ?, estado = ?, cnpj = ?, razao_social = ? where codigo = ?", fornecedor);
                JOptionPane.showMessageDialog(this, "Fornecedor alterado com sucesso.");
            } catch (Exception ex) {
                Logger.getLogger(frmCadastroFornecedor.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        btnSalvar.setEnabled(false);
        txtCodigo.setVisible(true);
        jLabelCodigo.setVisible(true);
        btnEditar.setEnabled(true);
        btnExcluir.setEnabled(true);
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        try {
            if (codigo == 0) {
                JOptionPane.showMessageDialog(this, "Selecione um fornecedor para excluir!");
                return;
            }
            Object[] codigo_cliente = new Object[1];
            codigo_cliente[0] = codigo;

            executaOperacao("delete from fornecedor where codigo = ?", codigo_cliente);
            JOptionPane.showMessageDialog(this, "Fornecedor excluído com sucesso!");
            txtCodigo.requestFocusInWindow();

        } catch (Exception ex) {
            Logger.getLogger(frmCadastroFornecedor.class.getName()).log(Level.SEVERE, null, ex);
        }

}//GEN-LAST:event_btnExcluirActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        frmCadastroFornecedor.this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        habilitarCampos();
        btnSalvar.setEnabled(true);
        btnExcluir.setEnabled(false);
        btnEditar.setEnabled(false);
    }//GEN-LAST:event_btnEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelCodigo;
    private javax.swing.JTextField txtCNPJ;
    private javax.swing.JTextField txtCidade;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtEstado;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtRazaoSocial;
    private javax.swing.JTextField txtTelefone1;
    private javax.swing.JTextField txtTelefone2;
    // End of variables declaration//GEN-END:variables
}
